<?php
session_start();
	require_once "includes/config.php";
	require_once "includes/pagination.php";
    $itemPerPage=10;	
	
	$sg = $_REQUEST['sg2'];
	$key = $_REQUEST['key'];
	//$state = $_GET['state'];
	//$city = $_GET['city'];
	//$priceFrom = $_GET['priceFrom'];
	//$priceTo = $_GET['priceTo'];
	
	if(!empty($sg)) 
	{	
	
	 
		$sqlSelPro="SELECT *, tbl_wholeseller_product.product_name, tbl_users.user_id 
					FROM tbl_wholeseller_product, tbl_users 
					WHERE tbl_users.user_id=tbl_wholeseller_product.posted_by ";	
	
		$searchIncludes="";														
		if($city!="" || $state!="") 
		{// City & state search starts
		  $searchIncludes="location";
		  if($city!="" && $state!="") 
			$sqlSelPro .= " AND tbl_users.state LIKE '%$state%' AND tbl_users.city LIKE '%$city%'";
		  else if($state!="")
		   		$sqlSelPro .= " AND tbl_users.state LIKE '%$state%'";
		  else if($city!="")
		   		$sqlSelPro .= " AND tbl_users.city LIKE '%$city%' ";					
			if(($priceFrom!="" && is_numeric($priceFrom)) || ($priceTo!=""  && is_numeric($priceTo)))
			 {// Price range including City & state search starts
				if($priceFrom!="" && $priceTo!="" && is_numeric($priceFrom) && is_numeric($priceTo))
					$sqlSelPro .= " AND tbl_wholeseller_product.product_price  BETWEEN $priceFrom AND $priceTo ";
				else if($priceFrom!="" && is_numeric($priceFrom))
			        $sqlSelPro .= " AND tbl_wholeseller_product.product_price >=$priceFrom ";  
				else if($priceTo!="" && is_numeric($priceTo))
			        $sqlSelPro .= " AND tbl_wholeseller_product.product_price <=$priceTo ";
		}// Price range including City & state search Ends	
		}// City & state search Ends
	    else if(($priceFrom!="" && is_numeric($priceFrom)) || ($priceTo!=""  && is_numeric($priceTo)))
		{// Price range search alone starts
		     $searchIncludes="pricerange";
		     if($priceFrom!="" && $priceTo!="" && is_numeric($priceFrom) && is_numeric($priceTo))
			   $sqlSelPro .= " AND tbl_wholeseller_product.product_price  BETWEEN $priceFrom AND $priceTo ";
			 else if($priceFrom!="" && is_numeric($priceFrom))
			       $sqlSelPro .= " AND tbl_wholeseller_product.product_price >=$priceFrom ";  
			 else if($priceTo!="" && is_numeric($priceTo))
			        $sqlSelPro .= " AND tbl_wholeseller_product.product_price <=$priceTo ";
		}// Price range search alone Ends
		
		 if($searchIncludes=="location")
		 {
		   if($key!='')
		     $sqlSelPro.=" AND tbl_wholeseller_product.product_name LIKE '%$key%' "; 
		   
		 }
		 else if($searchIncludes=="pricerange")
		 {
		   if($key!='')
		     $sqlSelPro.=" AND product_name LIKE '%$key%' "; 
		  
		  }
		  else
		  {
		    if($key!='')
		     $sqlSelPro.="  AND  tbl_wholeseller_product.product_name LIKE '%$key%' "; 
		   
		  }	
		 
		/* else
		{
		$sqlSelPro="SELECT * FROM tbl_product";
		$pagelink="srch&sg";
	} */
  }
	$pLink = "srchwhole=1&sg2=$sg&key=$key&st=$state&cty=$city&pf=$priceFrom&pt=$priceTo";	
	
	$pos     = strripos($sqlSelPro, "where");
	$curDate = date("Y-m-d G:i:s",time());
	//echo $curDate;
	
	//echo $sqlSelPro;
?>	
<div id='panel'>
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Search Results <?php if(!empty($searchFor)) echo " - ".$searchFor; ?></h2></td>
  </tr>
  <tr>
    <td>

		 <?php 		   
				   if(!empty($_GET['sortby'])) {
				   		if($_GET['sortby']=='lps') {
							$sqlSelPro .= " order by tbl_wholeseller_product.product_price ASC";		
						}
						else if($_GET['sortby']=='hps') {
							$sqlSelPro .= " order by tbl_wholeseller_product.product_price DESC";		
						}
						else if($_GET['sortby']=='bm') {
							$sqlSelPro .= " order by field(tbl_wholeseller_product.product_name, '$key') DESC";		
						}
						else if($_GET['sortby']=='gen') {
							$sqlSelPro .= " order by tbl_wholeseller_product.product_id ASC";		
						}
						
				   }		
				   $rstSelPro=mysql_query(getPagingQuery($sqlSelPro,$itemPerPage));			  
				   $num=mysql_num_rows($rstSelPro);				   
				   if($num==0)
					{					
					  	echo "No Products Found...";
					 
					}
				   else { ?>
				   		
					   <br><table width='90%' border='0' align='center' cellspacing='0'>
						       <tr>
							   	<td colspan='5'>&nbsp;									</td>
							   </tr>
							   <tr class='head-tr'>
								  <td height='26' width='68'>&nbsp;</td>
							      <td width='138'><strong>Product Code </strong></td>
							      <td width='138'> <strong>Product name </strong></td>
							     <td width='90'> <strong>--</strong> </td>
							     <td width='54'> <strong>Price</strong> </td>
				         </tr>
						<?php	
						
						 while($rowSelPro=mysql_fetch_array($rstSelPro))
					         {							   
						   $productId=$rowSelPro['product_id'];
						   $postedby=$rowSelPro['posted_by'];
						   $itemId=$rowSelPro['product_id'];
						   $productName=wordFormater($rowSelPro['product_name'],20);
						   $productTitle = $rowSelPro['product_name'];					   
						   $productPrice=$rowSelPro['product_price'];
						   $postedDate=$rowSelPro['posted_date'];
						    $productCode=$rowSelPro['product_code'];
						   
						   $pimage = getWholeProductImage($itemId,'p');
						   if($pimage['thumb']!="") {
						   		$productThumb = $pimage['thumb'];
						   } else {
						   		$productThumb="images/no_image.gif";	
						   }
						   ?>  
						   <tr>
						        <td height='106' valign="top">
						   			<div align='center'><img src='<?php echo $productThumb; ?>'></div>								 </td>								 
								 <td style='vertical-align:top; padding:10px;'><a href='index.php?wholeproducts=<?php echo $itemId; ?>' title='<?php echo $productTitle; ?>'> <?php echo $productCode; ?> </a> </td>
								 <td style='vertical-align:top; padding:10px;'> <a href='index.php?wholeproducts=<?php echo $itemId; ?>' title='<?php echo $productTitle; ?>'> <?php echo $productName; ?> </a> </td>
								 <td style='vertical-align:top; padding:10px;'><input type='image' src='images/buy.gif' style='cursor:pointer;' onClick=''></td>
								  <td style='vertical-align:top; padding:10px;'>$<?php echo number_format($productPrice,2); ?><br /><?php if($_SESSION['user_id_sess']) {?><A HREF="javascript:void(0)" onclick="window.open('sendowner.php?ownerid=<?php echo $postedby;?>&productid=<?php echo $productId;?>','welcome','width=500,height=300')">Message</A><?php } else {?><A HREF="index.php?signin">Message</A><?php }?></td>
						   </tr>	
							   <tr>
									 <td colspan='5'> <div style='border-top:1px solid #CCCCCC;'> </div> </td>
							   </tr>				 	
						 <?php } ?>				 
						</table>
	    <?php } ?>
	
	  <tr>
    		<td><div align="center"><?php
				   $link=$pLink;
				   echo $disp=getPagingLink($sqlSelPro,$itemPerPage,$link);							   
			 ?></div>
			</td>
  		</tr>
 	</table>
	</td>
  </tr>
</table>
</div>